package com.it.controller;

import com.it.common.Result;
import com.it.dao.SongListMapper;
import com.it.dao.TagMapMapper;
import com.it.domain.SongList;
import com.it.domain.Tags;
import com.it.domain.User;
import com.it.service.LoginService;
import com.it.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/tag")
@RestController
public class TagsController {

    @Autowired
    private TagsService tagsService;

    @PostMapping("/AddTags")
    public Object AddTags(@RequestBody Tags tags){

        if(tags.getName() == null || tags.getName() == ""){
            return Result.fail("405","标签为空");
        }
        else{
            return tagsService.AddTags(tags);
        }
    }

    @PostMapping("/SelectPage")
    public Object SelectPage(@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize, @RequestParam("name") String name){

        name = '%' + name + '%';
        Map<String,Object> res = new HashMap<String,Object>();
        pageNum = (pageNum-1)*pageSize;
        Integer total = tagsService.SelectTotal();
        List<Tags> data = tagsService.SelectPage(pageNum,pageSize,name);
        res.put("data",data);
        res.put("total",total);

        return res;
    }

    @GetMapping("SelectAll")
    public Result SelectAll(){

        if(tagsService.SelectAll() == null){
            return Result.fail("403","数据为空");
        }
        else{
            return Result.success("200",tagsService.SelectAll());
        }
    }

    @PostMapping("SelectTags")
    public List<Integer> SelectTags(@RequestParam String[] names){
        return tagsService.SelectTags(names);

    }

    @DeleteMapping("DeleteTag/{id}")
    public Object DeleteTag(@PathVariable Integer id){
        if(id == null){
            return Result.fail("404","数据问题");
        }else{
            return tagsService.DeleteTag(id);
        }
    }

    @GetMapping("GetTags")
    public List<Tags> GetTags(){
        return tagsService.GetTags();
    }
}
